# ________________________________________________________
# _*_ coding: utf-8 _*_
#
# Copyright (C) 2024_11_08 Yuyun Personal All Rights Reserved 
# @Project : python-question-solving
# @Time    : 8:29
# @Author  : Yuyun
# @File    : 定期删除文件或目录.py
# @IDE     : PyCharm
# _______________________________________________________



"""
    定期删除文件或者目录
        -统计文件或目录的ctime与mtime
        -取最大mtime文件
"""

import os
import time
import shutil
from apscheduler.schedulers.background import BlockingScheduler
import tkinter as tk
from tkinter import filedialog

class Soltion:
    def __init__(self, tardir):
        self.testdir = tardir
    def delete_file(self):
        files = os.listdir(self.testdir)

        #   计算mtime
        mtimes = []
        for file in files:
            paths = os.path.join(self.testdir, file)
            file_mtime = os.path.getmtime(paths)
            mtimes.append(file_mtime)
        max_mtime = max(mtimes)

        #   删除
        # for file in files:
        #     if file_mtime[files.index(file)] < max_mtime:
        #         paths = os.path.join(self.testdir, file)
        #         if os.path.isfile(paths):
        #             os.remove(paths)
        #         else:
        #             shutil.rmtree(paths)
class File_Dir:
    def file_dir_select(self):
        root = tk.Tk()
        #   隐藏主窗口
        root.withdraw()
        open_dialog_button = tk.Button(root, text="Open Directory")
        open_dialog_button.pack(pady=20)
        #   打开文件对话框
        file_path = filedialog.askdirectory()
        if file_path:
            root.quit()


if __name__ == '__main__':
    tardir = input()
    #   创建调度器
    scheduler = BlockingScheduler()

    solution = Soltion(tardir)
    #   添加任务
    scheduler.add_job(solution.delete_file(),"interval",hours = 2)

    #   满足条件执行任务
    scheduler.start()
